
<template>
     <el-input ref="myinput" v-model="currentValue" @blur="blurs" placeholder="" :autofocus="autofocus" v-if="!disableds" :readonly="disableds"  :class="inputClass" :style="styles"  size="small">
     </el-input>
    <div v-else style="padding:5px 15px;min-height:32px;">
        {{ currentValue }}
    </div>
</template>
 
<script>
    import filterXSS from "xss"
    export default {
        props:['id','value','styles','disabled','data','isreadonly','exdata','dataField','extype','exkey','isNotSave','gray','autofocus','blurChange'],
        name: "input-noborder",
        data(){
            return {
                currentValue:this.value,
                disableds:this.disabled,
                first:'Y',
                pId:"",
                cId:"",
                oldValue:"",
                amtStr:"",
                decimal:"",
                xssOption:{
                    stripIgnoreTagBody:true
                },
                isreadonlys:"N",
                inputClass:"no-border",
                
            }
        },
        created(){
            console.log("autofocus=========="+this.autofocus)
            if(this.autofocus){
                window.setTimeout(()=>{
                    console.log(this.$refs.myinput,"myinput===============")
                    this.$refs.myinput.focus();
                },300)
            }
            if(this.gray=="Y"){
                this.inputClass="no-border no-border-disabeld"
            }
           try{
             if(this.exkey.decimal){
                this.decimal=Number(this.exkey.decimal);
             }
           }catch(e){}
           
            
           this.setData();
            try{
                let disabled=this.exkey.disabled;
                let disableds=disabled.split("#");
                let opr=disableds[1];
                let id=disableds[0];
                let vals=disableds[2];
                let val_=this.data[this.dataField][this.exdata][id]
                if(opr=="等于"&&vals==val_){
                    this.disableds=true;
                }
                 if(opr=="不等于"&&vals!=val_){
                    this.disableds=true;
                }
            }catch(e){}
            try{
                if(this.exkey.amtStr){
                    this.amtStr=this.exkey.amtStr;
                }else{
                    this.amtStr=this.exkey;
                }
            }catch(e){}
            console.log("di===="+this.id+"=="+this.isreadonly)
            try{
                if(this.exkey.readonlys){
                    let isReadonly_="Y"
                    for(let i=0;i<this.exkey.readonlys.length;i++){
                        let ids=this.exkey.readonlys[i].id.split(".");
                        
                        let opr=this.exkey.readonlys[i].opr;
                        let vals=this.exkey.readonlys[i].val;
                        let val_="";
                        if(ids[0]=="本身"){
                            val_=this.currentValue;
                        }else{
                            val_=this.data[ids[0]][ids[1]];
                        }
                        
                        if(opr=="等于"&&vals!=val_){
                            //this.isreadonlys="Y";
                            isReadonly_="N"
                        }
                        if(opr=="不等于"&&vals==val_){
                            //this.isreadonlys="Y";
                            isReadonly_="N"
                        }

                    }
                    this.isreadonlys=isReadonly_;
                }
            }catch(e){}
            if(this.isreadonly=="Y"||this.isreadonlys=="Y")
                this.disableds=true;
            if(this.isreadonly=="A"){
                
                if(this.currentValue&&this.currentValue!="0"&&this.currentValue!="0.0"&&this.currentValue!="0.00"){
                    this.disableds=true;
                }
               // alert(this.disableds+"=="+this.currentValue)
            }
           console.log("disabled==="+this.disableds+"=="+this.id+"=="+this.currentValue)
        },
        mounted(){
            //this.$refs.myinput.style.color="red"
           
                try{
                    let color_val=this.data[this.dataField][this.exdata].color_val;
                    if(color_val){
                        this.$refs.myinput.$el.children[0].style.color=color_val;
                    }else{
                        this.$refs.myinput.$el.children[0].style.color="#606266"
                    }
                }catch(e){}
                
            
            
        },
       /*  computed:{
            dataFields:function(){
                console.log(this.exdata+"="+this.dataField+"="+this.id+"="+this.data[this.dataField][this.exdata][this.id])
                if(this.pId)
                    return this.data[this.pId][this.id];
                else if(this.dataField){
                    return this.data[this.dataField][this.exdata][this.id];
                }
            }
        },*/
        methods:{
            blurs(){
               
                try{
                    if(this.decimal!=undefined&&this.decimal!="undefined"&&this.decimal&&this.currentValue!=''){
                        this.currentValue=Number(this.currentValue).toFixed(this.decimal)
                    }
                }catch(e){}
                this.$emit("blur","");
                if(this.blurChange=="Y"){
                    let val=this.currentValue;
                    if(this.isNotSave=="Y"){
                        let data={};
                        data.id=this.id;
                        data.val=this.currentValue;
                        data.exdata=this.exdata;
                        this.$emit('input',JSON.stringify(data));
                    }else{
                        if(this.dataField){
                            let data_=this.data[this.dataField][this.exdata];
                            data_[this.id]=val;
                            if(this.extype=="amt"&&val!="-"){
                                if(!this.isRealNum(val)){
                                    this.currentValue="0";
                                }
                                let exkeys=this.amtStr.split(",");
                                let val_=Number(val);
                                let val2_=Number(this.data[this.dataField][this.exdata][exkeys[0]]);
                                data_[exkeys[1]]=val_*val2_;
                            }
                        
                            this.$set(this.data[this.dataField],this.exdata,data_);
                        }else{
                            this.$set(this.data[this.pId],this.cId,val)
                        }
                        let data={};
                        data.id=this.id;
                        data.val=val;
                        data.exdata=this.exdata;

                        this.$emit('input',JSON.stringify(data));
                        this.$emit('on-change',JSON.stringify(data));
                    }
                }
            },
            setBackColor(color){
               
                if(!color){
                    this.inputClass="no-border"
               }else{
                    this.inputClass="no-border no-border-disabeld"
               }
              // alert(this.inputClass)
            },
            setData(exdata,id,val){
               // console.log("this.dataField============"+this.dataField+"=="+this.exdata+"=="+this.id)
               let currentValue="";
                if(this.data){
                    let ids=this.id.split(".");
                    if(ids.length>1){
                        let p=ids[0];
                        let id_=ids[1];
                        this.pId=p;
                        this.cId=id_;
                        try{
                            currentValue=unescape(this.data[p][id_]);
                        }catch(e){}
                    }else{
                        if(this.dataField){
                            if(this.data[this.dataField][this.exdata][this.id])
                                currentValue=unescape(this.data[this.dataField][this.exdata][this.id]);
                            else{
                                currentValue="";
                            }
                        }else
                            currentValue= unescape(this.data[this.id]);
                    }
                
                }
             //this.data[exdata][id]=val;
               if(currentValue==undefined){
                    currentValue="";
               }
                
               
                try{
                    if(this.exkey&&this.exkey.valType){
                        if(this.exkey.valType=="int"){
                            currentValue=""+(parseInt(currentValue))
                        }else if(this.exkey.valType=="int2"){
                            currentValue=Math.ceil(Number(currentValue))
                        }
                    }
                }catch(e){}
               // console.log("this.decimal====="+this.decimal+"="+this.id)
               try{
                    if(this.decimal!=undefined&&this.decimal!="undefined"&&this.decimal&&currentValue!=''){
                        currentValue=Number(currentValue).toFixed(this.decimal)
                    }
                }catch(e){}
                this.currentValue=currentValue;
                this.oldValue=this.currentValue;
                
            },
            isRealNum(val){
                // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，
                　　if(val === "" || val ==null){
                        return false;
                　　}
                if(!isNaN(val)){　　　　
                    　　//对于空数组和只有一个数值成员的数组或全是数字组成的字符串，
                    　　//isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
                    //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
                    　　　 return true; 
                }else{ 
                　　return false; 
                } 

            }
        },
       
        watch:{
            currentValue: function (val) { //currentValue值变动就向父组件传值
               // console.log("this.extype==="+this.extype+"="+val);
               // console.log("SDDDDDDDDDDDDDDDDDDDDDDDDDDDSSSSSSSSSSSSSSSSSSDDDDDDDDDDDDDD");
                if(val==NaN||val=="NaN"){
                    val="";
                    this.currentValue=val;
                    this.oldValue=val;
                }
               if(val==this.oldValue||val=="-"){
                   return;
               }
               let currentValue=val;
               try{
                    if(this.exkey&&this.exkey.valType){
                        if(this.exkey.valType=="int"){
                            currentValue=""+(parseInt(currentValue))
                        }else if(this.exkey.valType=="int2"){
                            currentValue=Math.ceil(Number(currentValue))
                        }
                    }
                }catch(e){}
               // console.log("this.decimal====="+this.decimal+"="+this.id)
              
                
                console.log("currentValue===="+currentValue)
               currentValue=filterXSS(currentValue,this.xssOption);
               currentValue=currentValue.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/%3C/g,"&lt;").replace(/%3E/g,"&gt;")

               this.currentValue=currentValue;
               this.GLOBAL.isChange=true;
               this.oldValue=val;
                if(!val){
                    val="";
                }
              //  if(this.first!='Y'){
                  /*   let data={};
                    data.id=this.id;
                    data.val=val;
                    data.exdata=this.exdata;

                    this.$emit('input',JSON.stringify(data));
                    this.$emit('on-change',JSON.stringify(data));*/
                    
                    if(this.blurChange!="Y"){
                        if(this.isNotSave=="Y"){
                            let data={};
                            data.id=this.id;
                            data.val=val;
                            data.exdata=this.exdata;
                            this.$emit('input',JSON.stringify(data));
                        }else{
                            if(this.dataField){
                                let data_=this.data[this.dataField][this.exdata];
                                data_[this.id]=val;
                                if(this.extype=="amt"&&val!="-"){
                                    if(!this.isRealNum(val)){
                                        this.currentValue="0";
                                    }
                                    let exkeys=this.amtStr.split(",");
                                    let val_=Number(val);
                                    let val2_=Number(this.data[this.dataField][this.exdata][exkeys[0]]);
                                    data_[exkeys[1]]=val_*val2_;
                                }
                            
                                this.$set(this.data[this.dataField],this.exdata,data_);
                            }else{
                                this.$set(this.data[this.pId],this.cId,val)
                            }
                            let data={};
                            data.id=this.id;
                            data.val=val;
                            data.exdata=this.exdata;

                            this.$emit('input',JSON.stringify(data));
                            this.$emit('on-change',JSON.stringify(data));
                        }
                        
                    }
                    

                    
               // }
               
                this.first='N'
            },
            value:function (val) { //对值进行验证0
                
            },
             data:{
                handler: function (data) {
                    let newVal="";
                    if(this.pId)
                        newVal= this.data[this.pId][this.id];
                   
                     else if(this.dataField){
                        newVal= this.data[this.dataField][this.exdata][this.id];
                    }

                    //console.log("change==="+"="+this.oldValue+"="+newVal+"=="+this.exdata+"=="+this.id)
                    if(this.oldValue!=newVal){
                        //setData
                        this.setData()
                    }
                    try{
                        let color_val=this.data[this.dataField][this.exdata].color_val;
                        //console.log("this.exdata====="+this.dataField+"=="+this.exdata+"=="+color_val)
                        if(color_val){
                            this.$refs.myinput.$el.children[0].style.color=color_val;
                        }else{
                            this.$refs.myinput.$el.children[0].style.color="#606266"
                        }
                    }catch(e){}
                    
                
                },      
                deep: true    //深度监听
            }

        }
    }
</script>
<style scoped>
    .el-input__inner{
        height:25px;
        border:none;
    }
    input{
        height:25px;
    }
    .no-border>>>input{
        background:#fff;
    }
    .no-border-disabeld>>>input{
        background:#ddd;
    }
</style>